package com.kiln.juxingqs.ui

import android.content.Intent
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.OrderFragmentPageAdapter
import com.kiln.juxingqs.databinding.ActivityMeorderBinding
import com.kiln.juxingqs.dialog.SelectPaytypeDialog2
import com.kiln.juxingqs.ui.vm.MeOrderViewModel

class RechargeOrderActivity : BaseMiddleActivity<MeOrderViewModel, ActivityMeorderBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_meorder
    }

    val adapter: OrderFragmentPageAdapter by lazy {
        OrderFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initView() {
//        EventBus.getDefault().register(this)
        binding.event = this
        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)

//        when (intent.getIntExtra("index", 0)) {
//            0 -> {
////                binding.tabWrap.getChildAt(0).isSelected = true
//            }
//            1 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(1, false)
//                }, 10)
//            }
//        }
    }

    override fun observerUI() {
        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("取消成功")
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()
                }
            }
        })

        viewModel.rechargeCall.observe(this) {
            it?.let {
                if (it.isSuccess) {
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()
                    var paymentUrl = it.data?.pay_url
                    var paymentId = it.data?.payment_id

                    if (it.data?.w_type.equals("1")) {
                        startActivity(Intent(this, RechargeActivity::class.java))

                    } else if (it.data?.w_type.equals("3")) {
                        if (paymentId.isNullOrEmpty()) {
                            toast("payment_id参数空")
                        } else {
                            paymentId?.let {
                                startActivity(
                                    Intent(
                                        this@RechargeOrderActivity,
                                        Bank3CodeActivity::class.java
                                    ).apply {
                                        putExtra("payment_id", it)
                                    })
                                finish()
                            }
                        }
                    } else if (it.data?.w_type.equals("4") || it.data?.w_type.equals("6")) {
                        paymentUrl?.let {
                            val intent = Intent(this, WebPayActivity::class.java)
                                .apply {
                                    putExtra("adInfo", it)
                                    putExtra("title", "支付")
                                }
                            startActivity(intent)
                            finish()
                        }
                    } else if (it.data?.w_type.equals("5")) {
                        startActivity(Intent(this, DePayShowActivity::class.java).apply {
                            putExtra("remark", it.data!!.oid)
                            putExtra("price", it.data!!.price)
                        })
                    } else {
                        paymentUrl?.let {
                            val intent = Intent(this, WebPayActivity::class.java)
                                .apply {
                                    putExtra("adInfo", it)
                                    putExtra("title", "支付")
                                }
                            startActivity(intent)
                            finish()
                        }
                    }


//                    SelectPaytypeDialog2(this).apply {
//                        one = { it, txt ->
//                            if (it == 1) {
//                                startActivity(Intent(this@RechargeOrderActivity, RechargeActivity::class.java))
//
//                            } else if (it == 3) {
//                                if (paymentId.isNullOrEmpty()) {
//                                    toast("payment_id参数空")
//                                } else {
//                                    paymentId?.let {
//                                        startActivity(
//                                            Intent(
//                                                this@RechargeOrderActivity,
//                                                Bank3CodeActivity::class.java
//                                            ).apply {
//                                                putExtra("payment_id", it)
//                                            })
//                                        finish()
//                                    }
//                                }
//                            } else {
//                                val paymentUrl1 = paymentUrl
//                                paymentUrl1?.let {
//                                    val intent =
//                                        Intent(this@RechargeOrderActivity, WebPayActivity::class.java)
//                                            .apply {
//                                                putExtra("adInfo", it)
//                                                putExtra("title", "支付")
//                                            }
//                                    startActivity(intent)
//                                }
//                            }
//                            finish()
//                        }
//                    }.show()
                }
            }
        }
    }

}